package com.ukit.todolist_xml

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Build
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import android.widget.EditText
import androidx.annotation.RequiresApi
import com.ukit.todolist_xml.RoomRepository.ToDoItem

class AddItemDialogFragment : DialogFragment() {

    @RequiresApi(Build.VERSION_CODES.O)
    @SuppressLint("MissingInflatedId")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder(activity)
        val inflater = requireActivity().layoutInflater
        val view = inflater.inflate(R.layout.dialog_add_item, null)
        val editText = view.findViewById<EditText>(R.id.editTextNewItem)

        builder.setView(view)
            .setTitle("Добавить новую задачу")
            .setPositiveButton("Добавить",
                DialogInterface.OnClickListener { dialog, id ->
                    val newItemText = editText.text.toString()
                    (activity as MainActivity).addItemToList(newItemText)
                })
            .setNegativeButton("Отмена",
                DialogInterface.OnClickListener { dialog, id ->
                    dialog.cancel()
                })
        return builder.create()
    }
}
